﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace BenjaminSchroeter.GeoNames.TestApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnFindToponym_Click(object sender, EventArgs e)
        {
            decimal lat;
            decimal log;
            string[] classes;
            string[] codes;
            try
            {
                lat = Decimal.Parse(this.textBox1.Text);
                log = Decimal.Parse(this.textBox2.Text);

                classes = this.txtClasses.Text.Split(' ').Select(x => x.Trim()).ToArray();
                codes = this.txtCodes.Text.Split(' ').Select(x => x.Trim()).ToArray();
            }
            catch 
            {
                return;
            }

            Geoname g = GeoNamesOrgWebservice.FindNearby(lat, log,classes,codes,GeoNamesDataStyle.Full);
               
            this.treeView1.Nodes.Clear();
            this.treeView1.Nodes.Add(CreateTreeNode(g));

        }
        private void btnFindPopulatedPlace_Click(object sender, EventArgs e)
        {
            decimal lat;
            decimal log;
            try
            {
                lat = Decimal.Parse(this.textBox1.Text);
                log = Decimal.Parse(this.textBox2.Text);
            }
            catch
            {
                return;
            }

            Geoname g = GeoNamesOrgWebservice.FindNearbyPlaceName(lat, log, this.numRadius.Value, GeoNamesDataStyle.Full);

            this.treeView1.Nodes.Clear();
            this.treeView1.Nodes.Add(CreateTreeNode(g));
        }
        private void btnFindWikipedia_Click(object sender, EventArgs e)
        {
            decimal lat;
            decimal log;
            try
            {
                lat = Decimal.Parse(this.textBox1.Text);
                log = Decimal.Parse(this.textBox2.Text);
            }
            catch
            {
                return;
            }

            List<WikipediaArticle> a = GeoNamesOrgWebservice.FindNearbyWikipedia(lat, log, this.txtWikipediaLang.Text, this.numWikipediaRadius.Value, (int)this.numWikipediaRows.Value);

            this.treeView1.Nodes.Clear();
            foreach (WikipediaArticle article in a)
                this.treeView1.Nodes.Add(CreateTreeNode(article));
        }

        private void btnGet_Click(object sender, EventArgs e)
        {
            int id;
            try
            {
                id = int.Parse(this.txtGenonameId.Text);
            }
            catch
            {
                return;
            }
            Geoname g = GeoNamesOrgWebservice.Get(id);

            this.treeView1.Nodes.Clear();
            this.treeView1.Nodes.Add(CreateTreeNode(g));
        }
        private void btnGetHierarchy_Click(object sender, EventArgs e)
        {
            if ( this.treeView1.SelectedNode == null )
                return;
            Geoname x = this.treeView1.SelectedNode.Tag as Geoname;
            if (x == null)
                return;

            var hier = x.Hierarchy(GeoNamesDataStyle.Full);

            this.treeView1.Nodes.Clear();
            TreeNodeCollection parent = this.treeView1.Nodes;
            foreach (Geoname geoname in hier)
            {
                TreeNode n = CreateTreeNode(geoname);

                parent.Add(n);
                parent = n.Nodes;
            }

            this.treeView1.ExpandAll();

        }
        private void btnGetChildren_Click(object sender, EventArgs e)
        {
            if (this.treeView1.SelectedNode == null)
                return;
            Geoname x = this.treeView1.SelectedNode.Tag as Geoname;
            if (x == null)
                return;

            var hier = x.Children(GeoNamesDataStyle.Full);

            foreach (Geoname geoname in hier)
                this.treeView1.SelectedNode.Nodes.Add(CreateTreeNode(geoname));

            this.treeView1.SelectedNode.Expand();
        }

        private void btnGetWeather_Click(object sender, EventArgs e)
        {
            decimal lat;
            decimal log;
            try
            {
                lat = Decimal.Parse(this.textBox1.Text);
                log = Decimal.Parse(this.textBox2.Text);
            }
            catch
            {
                return;
            }

            WeatherObservation w = GeoNamesOrgWebservice.FindNearbyWeather(lat, log);

            this.treeView1.Nodes.Clear();
            this.treeView1.Nodes.Add(CreateTreeNode(w));
        }

        private void btnGtopo30_Click(object sender, EventArgs e)
        {
            decimal lat;
            decimal log;
            try
            {
                lat = Decimal.Parse(this.textBox1.Text);
                log = Decimal.Parse(this.textBox2.Text);
            }
            catch
            {
                return;
            }

            this.txtElevation.Text = GeoNamesOrgWebservice.GetElevation(lat, log, ElevationModel.GTOPO30).ToString();
        }

        private void btnSrtm3_Click(object sender, EventArgs e)
        {
            decimal lat;
            decimal log;
            try
            {
                lat = Decimal.Parse(this.textBox1.Text);
                log = Decimal.Parse(this.textBox2.Text);
            }
            catch
            {
                return;
            }

            this.txtElevation.Text = GeoNamesOrgWebservice.GetElevation(lat, log, ElevationModel.SRTM3).ToString();
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (this.treeView1.SelectedNode == null)
                return;

            this.propertyGrid2.SelectedObject = this.treeView1.SelectedNode.Tag;

            this.btnGetHierarchy.Enabled = this.treeView1.SelectedNode.Tag is Geoname;
            this.btnGetChildren.Enabled = this.treeView1.SelectedNode.Tag is Geoname;

            Geoname x = this.treeView1.SelectedNode.Tag as Geoname;
            if (x == null)
                return;
            this.listView1.Items.Clear();
            foreach (AlternateName name in x.AlternateNames())
                this.listView1.Items.Add(new ListViewItem(name.ToString()));

            this.textBox1.Text = x.Latitude.ToString();
            this.textBox2.Text = x.Longitude.ToString();
        }
        private TreeNode CreateTreeNode(Geoname geoname)
        {
            string str = string.Format("{0} ({1}, {2})", geoname.Name, geoname.Fcl, geoname.Fcode);

            TreeNode n = new TreeNode(str);
            n.Tag = geoname;
            return n;
        }
        private TreeNode CreateTreeNode(WikipediaArticle article)
        {
            TreeNode n = new TreeNode(article.Title);
            n.Tag = article;
            return n;
        }
        private TreeNode CreateTreeNode(WeatherObservation article)
        {
            TreeNode n = new TreeNode(article.Observation);
            n.Tag = article;
            return n;
        }
    }
}